const Koa = require('koa')
var http = require('http');
const app = new Koa()
const views = require('koa-views')
const koaBody = require('koa-body')
const users = require('./routes/users')
const socketio = require("socket.io"); 


app.use(koaBody({multipart: true}));
app.use(require('koa-static')(__dirname + '/public/house_picture'))

app.use(function(ctx, next) {
    ctx.response.set("Access-Control-Allow-Origin", "*");
    ctx.response.set("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    ctx.response.set("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    if(ctx.request.method=="OPTIONS") ctx.response.status = 200;/*让options请求快速返回*/
    else  next();
  });

app.use(users.routes(), users.allowedMethods())
var server = http.createServer(app.callback());
var io = require('socket.io')(server);
io.on('connection', function (socket) {
  global.socket = socket;
  console.log('客户端链接成功')
  socket.emit('customEmit', '链接socket 成功');
});
// setInterval(function() {
//     socket.emit('customEmit', { hello: 'world' });
// }, 1*1000);
server.listen(3000);
